package com.blogsoho.taurus.controller.admin;

import com.blogsoho.taurus.constants.URIMapping;
import com.blogsoho.taurus.vm.ContentViewModel;
import com.blogsoho.taurus.vm.ResponsePagerWrapper;
import com.blogsoho.taurus.vm.ResponseWrapper;
import com.blogsoho.taurus.vo.ContentVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by user on 2017/6/6.
 */
@RequestMapping(URIMapping.ADMIN_ARTICLE)
@Controller
public class ArticleController extends BaseAdminController {


    @GetMapping("add")
    public String addArticleView(Model model) throws Exception {
        model.addAttribute("category", categoryService.getAllCategory());
        return "admin/article_add";
    }

    @GetMapping("list")
    public String listArticleView(Model model) throws Exception {
        return "admin/article_list";
    }

    @GetMapping("{id:\\d+}")
    public String editArticleView(Model model, @PathVariable int id) throws Exception {
        ResponseWrapper<ContentVO> responseWrapper = contentService.getContentById(id);
        model.addAttribute("category", categoryService.getAllCategory());
        if (responseWrapper.isSuccess()) {
            model.addAttribute("content", responseWrapper.getData());
        }
        return "admin/article_edit";
    }

    @PostMapping("add")
    @ResponseBody
    public ResponseWrapper<?> addArticle(ContentViewModel content) {
        return contentService.addContent(content);
    }

    @PostMapping("list")
    @ResponseBody
    public ResponsePagerWrapper<List<ContentVO>> getAllContent(int start, int length, int draw) {
        ResponsePagerWrapper response = contentService.getAllContent(start, length);
        response.setDraw(draw);
        return response;
    }

    @PostMapping("edit")
    @ResponseBody
    public ResponseWrapper<?> updateArticle(ContentViewModel content) {
        return contentService.updateContent(content);
    }
}
